import CryptoJS from "crypto-js";
// https://cryptojs.gitbook.io/docs/

/*****
 * string 需要加密的字符串
 * keyStr 密钥
 * mode: CryptoJS.mode.ECB 加密模式ECB
 * padding: CryptoJS.pad.Pkcs7 填充方式Pkcs7
 * */ 
// 加密
const enCrypto = (string,keyStr = null) => {
    if(!keyStr){
        console.log(`The enCrypto function "keyStr" parameter is incorrect`)
        return
    }
    const key = CryptoJS.enc.Base64.parse(keyStr);
    return CryptoJS.AES.encrypt(string, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    }).toString()
}
// 解密
const deCrypt = (word, keyStr = null) => {
    if(!keyStr){
        console.log(`The deCrypt function "keyStr" parameter is incorrect`)
        return
    }
    const key = CryptoJS.enc.Base64.parse(keyStr);
    const decodedData = CryptoJS.AES.decrypt(word, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    })
    return decodedData.toString(CryptoJS.enc.Utf8);
};
export{
    enCrypto,
    deCrypt
}